#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QThread>
#include <QTimer>
#include <QModelIndex>

#include "common.h"
#include "gcs.h"
#include "networktests.h"
#include "AHWidget/include/AH.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    GCS _GCSObject;

    AHclass *AH_Core;

    NetworkTests DEBUG_NetworkTests;
    NetworkTests _network1Tests;
    NetworkTests _network2Tests;
    NetworkTests _internetTests;
    NetworkTests _arduinoTests;
    NetworkTests _VICONTests;

    bool _VICONcheckIsStillRunning;
    bool _ArduinocheckIsStillRunning;
    bool _ArduinoCanBePinged;
    bool _VICONCanBePinged;

    QTimer _ArduinoConnectionCheckTimer;
    QTimer _liveLogUpdateTimer;
    QTimer _numericalDisplayUpdateTimer;
    QTimer _VICONConnectionCheckTimer;
    QTimer _internetConnectionCheckTimer;
    QTimer _connectionStatusUpdateTimer;
    QTimer _AHUpdateTimer;
    QTimer _network1ConnectionCheckTimer;
    QTimer _network2ConnectionCheckTimer;

    QThread _GCSThread;
    QThread _network1TestThread;
    QThread _network2TestThread;
    QThread _internetTestThread;
    QThread _arduinoTestThread;
    QThread _VICONTestThread;
    QThread DEBUG_NetworkTestsThread;

    bool _network1CheckIsStillRunning;
    bool _network2CheckIsStillRunning;
    bool _internetCheckIsStillRunning;

    bool _Network1IsConnected;
    bool _Network2IsConnected;
    bool _InternetIsConnected;
    bool _VICONIsConnected;
    bool _ArduinoIsConnected;

    int	_Network1IPAddress[4];
    int	_Network2IPAddress[4];

    QString _Network1Name;
    QString _Network2Name;

    void StartArtificialHorizon();
    void StopArtificialHorizon();

    void StartConnectionLoops();
    void StopConnectionLoops();

    void LoadSettingsFromFile();
    void SaveSettingsToFile();

    void ChangeLiveLogUpdateFrequency(int updateRate);

    void ToggleFileLogging();

    void ResetRollGainValues();
    void ResetPitchGainValues();
    void ResetYawGainValues();

    void SaveGainValues();

private slots:
    void DEBUG_debugMessage(QString message);
    void DEBUG_pingResult(float pingResult);
    void DEBUG_interfaceTestResult(int *ipAddress);

    void UpdateLogList();

    void DoNetwork1ConnectionCheck();
    void DoNetwork2ConnectionCheck();
    void DoInternetConnectionCheck();
    void DoVICONConnectionCheck();
    void DoArduinoConnectionCheck();

    void ProcessNetwork1ConnectionCheck(int *ipAddress);
    void ProcessNetwork2ConnectionCheck(int *ipAddress);
    void ProcessInternetConnectionCheck(bool testPassed);
    void ProcessArduinoConnectionCheck(float pingResult);
    void ProcessVICONConnectionCheck(float pingResult);

    void DoConnectionStatusDisplayUpdate();

    void updateAH();

    void UpdateLiveLog();

    void updateNumericalDisplays();

    void RespondAeroQuadIPAddressChanged();
    void RespondVICONIPAddressChanged();
    
    void DoArtificialHorizonUpdate();

    void on_actionQuit_triggered();

    void on_action_viewLogging_triggered();

    void on_action_viewControlValues_triggered();

    void on_action_viewConnections_triggered();

    void on_action_viewHelp_triggered();

    void on_action_viewDebug_triggered();

    void on_button_DEBUG_Ping_clicked();

    void on_button_DEBUG_Interface_clicked();

    void on_button_changeIPVICON_clicked();

    void on_button_changeIPArduino_clicked();

    void on_slide_sampleFreq_sliderMoved(int position);

    void on_spin_sampleFreq_valueChanged(int );

    void on_button_toggleFileLogging_clicked();

    void on_actionToggle_File_Logging_triggered();

    void on_button_changeFileLogSaveLocation_clicked();

    void on_list_fileLogList_doubleClicked(QModelIndex index);

    void on_button_DEBUG_AH_clicked();

    void on_horizontalSlider_sliderMoved(int position);

    void on_horizontalSlider_2_sliderMoved(int position);

signals:
    void UIIsClosing();

    void DEBUG_Do_runPingTest(int *ipAddress);
    void DEBUG_Do_runNetworkInterfaceTest(QString interfaceName);

    void Do_AH_update(double phi, double theta, double p, double q);

    void Do_Network1_runNetworkInterfaceTest(QString interfaceName);
    void Do_Network2_runNetworkInterfaceTest(QString interfaceName);

    void Do_Internet_runDNSTest();

    void Do_Arduino_runPingTest(int *ipAddress);
    void Do_VICON_runPingTest(int *ipAddress);

    void Do_AeroQuad_ChangeIP(int *newIPAddress);
    void Do_VICON_ChangeIP(int *newIPAddress);

};

#endif // MAINWINDOW_H
